package com.darrenchan.leetcode;

import java.util.*;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-03-21 18:51
 */
public class QLint545 {

    private Queue<Integer> queue;
    private int k;
    /*
     * @param k: An integer
     */
    public QLint545(int k) {
        queue = new PriorityQueue<>();
        this.k = k;
    }

    /*
     * @param num: Number to be added
     * @return: nothing
     */
    public void add(int num) {
        if(queue.size() < k) {
            queue.add(num);
        } else {
            if(queue.peek() < num) {
                queue.poll();
                queue.add(num);
            }
        }
    }

    /*
     * @return: Top k element
     */
    public List<Integer> topk() {
        List<Integer> res = new ArrayList<>();
        for (Integer integer : queue) {
            res.add(integer);
        }

        Collections.sort(res, (o1, o2) -> o2 - o1);
        return res;
    }
}
